import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/industry_dao.dart';
// import 'package:hktk_app/decoration/chapter_tab_decoration.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/util/data_util.dart';

import 'package:hktk_app/util/view_util.dart';

/// 科目选择组件
class ChapterTitle2Widget extends StatefulWidget {
  /// tabBar 控制器
  final TabController tabController;

  /// tab相关数据
  final List<CategoryModel> subList;

  /// 返回的对应页面
  // final Widget onPage;
  final String pageName;
  final Map<String, dynamic>? arguments;

  const ChapterTitle2Widget({
    super.key,
    required this.tabController,
    required this.subList,
    required this.pageName,
    this.arguments,
  });

  @override
  State<ChapterTitle2Widget> createState() => _ChapterTitle2WidgetState();
}

class _ChapterTitle2WidgetState extends State<ChapterTitle2Widget> {
  /// 选中的下标
  late int _index;

  @override
  void initState() {
    super.initState();
    _index = IndustryDao.getSelectCategoryId() ?? 0;
    // tab滑动点击监听
    widget.tabController.addListener(() {
      if (widget.tabController.index == widget.tabController.animation!.value) {
        int index = widget.tabController.index;

        dataUtilComm.dataModel.categoryId = widget.subList[index].id!;
        dataUtilComm.dataModel.categoryName = widget.subList[index].name!;
        setState(() {
          _index = index;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 12),
      child: Row(

        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 1.sw-24-24,
            height: 46,
            alignment: Alignment.topLeft,
            child: _tabBarItem(),
          ),
          _iconItem(),
        ],
      ),
    );
  }

  /// tabBar组件
  _tabBarItem() {
    //BankTitleWidget
    return TabBar(

        // indicator: ChapterTagDecoration(
        //   isAtLast: _index == widget.subList.length - 1,
        // ),

        controller: widget.tabController,
        tabs: _tabItems(),
        // tab是否可以在水平方向上滚动
        isScrollable: true,
        // 从左到右进行排列，设置tab左侧对齐，去除左侧偏移
        tabAlignment: TabAlignment.start,
        // 分隔线高度
        dividerHeight: 0,
        // tab间距修改
        labelPadding: EdgeInsets.zero,
        // 选中样式
        indicator: BoxDecoration());
  }

  /// 显示对应的tab
  _tabItems() {
    List<Widget> widgets = [];
    int length = widget.subList.length;
    for (int i = 0; i < length; i++) {
      CategoryModel model = widget.subList[i];
      widgets.add(Tab(
        child: Padding(
          padding: EdgeInsets.only(right: i != length - 1 ? 48 : 10),
          child: Text(
            model.name!,
            style: TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.w400,
              color: _index == i ? colorBlue1 : colorBlack1,
            ),
          ),
        ),
      ));
    }
    return widgets;
  }

  /// 右边图标组件
  _iconItem() {
    return GestureDetector(
      onTap: _onTapFunIcon,
      child: SvgPicture.asset(
        "images/my/chapter_title.svg",
        width: 24,
        height: 24,
      ),
    );
  }

  /// 图标点击方法
  _onTapFunIcon() {
    debugPrint("你点击了 科目标题 图标 ===> ");
    // 通过内存获取
    List<CategoryModel> list = IndustryDao.getCategoryModels();
    // 跳转选择科目页面

    Navigator.pushNamed(context, "/select-category", arguments: {
      "subjectList": list,
      "buyCategorys": null,
      "pageIndex": 0,
      "pageName": widget.pageName,
      "onPage": Container(),
    });
  }
}
